﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace DMSkin.WeChat.Controls
{
    public class MessageBlock : Control
    {
        public static readonly DependencyProperty ImgSourceProperty = DependencyProperty.Register("ImgSource", typeof(ImageSource), typeof(MessageBlock), new PropertyMetadata(null, OnPropertyChanged));
        
        /// <summary>
        /// 头像
        /// </summary>
        [Description("头像"), Category("DMSkin")]
        public ImageSource ImgSource
        {
            get { return (ImageSource)GetValue(ImgSourceProperty); }
            set { SetValue(ImgSourceProperty, value); }
        }

        public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(MessageBlock), new PropertyMetadata(12.0));

        /// <summary>
        /// 图片宽度
        /// </summary>
        public double ImageWidth
        {
            get { return (double)GetValue(ImageWidthProperty); }
            set { SetValue(ImageWidthProperty, value); }
        }

        public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(MessageBlock), new PropertyMetadata(12.0));
        
        /// <summary>
        /// 图片高度
        /// </summary>
        public double ImageHeight
        {
            get { return (double)GetValue(ImageHeightProperty); }
            set { SetValue(ImageHeightProperty, value); }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MessageBlock),new PropertyMetadata(""));

        /// <summary>
        /// 文字
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty IsSelfProperty = DependencyProperty.Register("IsSelf", typeof(string), typeof(MessageBlock), new PropertyMetadata("Self", OnPropertyChanged));

        public string IsSelf
        {
            get { return (string)GetValue(IsSelfProperty); }
            set 
            { 
                if (value == "Self")
                {
                    LeftVisibilty = Visibility.Visible;
                    RighVisibilty = Visibility.Collapsed;
                }
                else
                {
                    LeftVisibilty = Visibility.Collapsed;
                    RighVisibilty = Visibility.Visible;
                }
                SetValue(IsSelfProperty, value);
            }
        }

        public static readonly DependencyProperty LeftVisibiltyProperty = DependencyProperty.Register("LeftVisibilty", typeof(Visibility), typeof(MessageBlock), new PropertyMetadata(Visibility.Visible, OnPropertyChanged));

        public Visibility LeftVisibilty
        {
            get { return (Visibility)GetValue(LeftVisibiltyProperty); }
            set { SetValue(LeftVisibiltyProperty, value); }
        }

        public static readonly DependencyProperty RighVisibiltyProperty = DependencyProperty.Register("RighVisibilty", typeof(Visibility), typeof(MessageBlock), new PropertyMetadata(Visibility.Collapsed, OnPropertyChanged));

        public Visibility RighVisibilty
        {
            get { return (Visibility)GetValue(RighVisibiltyProperty); }
            set { SetValue(RighVisibiltyProperty, value); }
        }

        private static void OnPropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            MessageBlock message = dependency as MessageBlock;
            if (args.Property.Name.Equals("IsSelf"))
                message.IsSelf = (string)args.NewValue;
            else if (args.Property.Name.Equals("LeftVisibilty"))
                message.LeftVisibilty = (Visibility)args.NewValue;
            else if (args.Property.Name.Equals("RighVisibilty"))
                message.RighVisibilty = (Visibility)args.NewValue;
            else if (args.Property.Name.Equals("ImgSource"))
                message.ImgSource = (ImageSource)args.NewValue;
        }
    }
}
